﻿package fwk.siteManager.managers
{
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import fwk.siteManager.Template;
	import fwk.siteManager.events.TemplateEvent;
	import fwk.siteManager.DisplayElement;
	
	public class TemplateModuleManager extends Template
	{
		
		private var _tabElements:Array;
		
		
		public function TemplateModuleManager(){
			
			
			trace("new TemplateModuleManager");
			
			_tabElements = new Array();
			
		}
		
		override public function init():void 
		{
	

			
			traiteElements(this.xmlTemplate);
				
			
		}
		
		private function traiteElements(xmlParam:XML)
		{
			for (var i:int = 0; i < xmlPage.content.children().length(); i++)
			{

				var xmlContenuElement:XML = xmlPage.content.children()[i];
				var nameNode:String = xmlContenuElement.name();
				
				
				try {
					
					
					var xmlElementTemplate:XML = xmlParam.children().(@id == nameNode)[0];
					

					if (xmlElementTemplate != null) {
						
						trace("xmlElementTemplate.@id " + xmlElementTemplate.@id);
						var clip:Object = ressources[xmlElementTemplate.@id] as Object;
						trace("clip " + clip);
						//this.addChild(clip as DisplayObject);
						
						if (clip is DisplayElement)
						{
							
							trace("  ");
							trace("-->>  new Element " +  xmlContenuElement.@id);
							trace("  ");
							trace("  ");
							
							trace("xmlElementTemplate " + xmlElementTemplate);
							trace("xmlContenuElement " + xmlContenuElement);
							
							clip._init(xmlElementTemplate.@id, xmlElementTemplate, xmlContenuElement);
							clip.addEventListener(TemplateEvent.CLOSED, onCloseElementFinish);
							tabDisplayElements.push(clip);

							
						}
					}
				}catch (e) {
					trace("XXXXX il n'y  pas de noeud template correspondant au noeud contenu " + xmlContenuElement.name());
				}
				
			}	
			
			super.init();
		}
		
		override public function onCloseElementFinish(evt:TemplateEvent):void
		{
			super.onCloseElementFinish(evt);
			
		}
	}
	
}